package com.example.seckill.controller;

import cn.hutool.core.util.StrUtil;
import com.example.seckill.controller.co.OrderCO;
import com.example.seckill.controller.co.UserCO;
import com.example.seckill.error.BusinessException;
import com.example.seckill.error.CommonErrorEnum;
import com.example.seckill.response.CommonReturnType;
import com.example.seckill.service.OrderInfoService;
import com.example.seckill.service.model.UserModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderInfoService orderInfoService;


    @PostMapping("create")
    public CommonReturnType create(@RequestBody OrderCO orderCO, HttpSession httpSession) {
        log.info("创建订单");
        Boolean isLogin = (Boolean) httpSession.getAttribute("IS_LOGIN");
        if (isLogin == null || !isLogin) {
            throw new BusinessException(CommonErrorEnum.USER_NOT_LOGIN);
        }
        UserModel userModel= (UserModel) httpSession.getAttribute("LOGIN_USER");
        orderInfoService.createOrder(userModel.getId(),orderCO);
        return CommonReturnType.create(null);
    }
}
